/*****************************************************************************/
/* Cookie functions */
/*****************************************************************************/
function getCookie(name) {

    var strArg = name + "=";
    var iArgLength = strArg.length;
    var iCookiesLength = document.cookie.length;

    var i = 0;
    var iEnd;
    
    while (i < iCookiesLength) {
        var j = i + iArgLength;
        if (document.cookie.substring(i, j) == strArg) {
            iEnd = document.cookie.indexOf(";", j);
            if (iEnd == -1) {
                iEnd = document.cookie.length;
            }
            return decodeURIComponent(document.cookie.substring(j, iEnd)).replace(/[+]/g, " ");
        }
        i++;
    }
    return "";
}
/*****************************************************************************/
function deleteCookie(name) {
    document.cookie = strName + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
/*****************************************************************************/
/* Popup functions */
/*****************************************************************************/
/* displays a popup message at top of window for a temporary time */
/* overlays other div */
function displayPopup(strMessage, strAlign, iTime) {
    
    /* display pop-up message */
    var divPopup = document.createElement("div");
    divPopup.innerHTML = strMessage;
   
    divPopup.setAttribute("id", "divPopup");
    divPopup.style.zIndex = "2";
    divPopup.style.top = "0"; divPopup.style.left = "0"
    //divPopup.style.left = getPosition(document.getElementById("divTitle"), "offsetLeft") + "px";

    divPopup.style.width = document.body.offsetWidth + 'px';
    divPopup.style.height = "20px";
    divPopup.style.fontSize = "14px"; divPopup.style.fontWeight="bold";
    
    divPopup.style.marginLeft = "auto"; divPopup.style.marginRight = "auto";
    divPopup.style.color = "#FFFFFF";
    divPopup.style.textAlign = strAlign;
    divPopup.style.backgroundImage = "url(images/message.png)";
    divPopup.style.backgroundRepeat = "repeat-x";
    
    divPopup.style.padding = "10px";
    divPopup.style.display = "block";
    divPopup.style.position = "fixed";
        
    document.body.insertBefore(divPopup, document.getElementById("divTitle"));
        
    // display temporarily
    setTimeout("hidePopup(document.getElementById('divPopup'));", iTime);
}
/*****************************************************************************/
/* remove the popup message */
function hidePopup(popup) {
    document.body.removeChild(popup);
}

/*****************************************************************************/
/* Generic div helpers */
/*****************************************************************************/
/* remove all immediate children of a DOM element */
function removeChildren(node) {
    
    if (node == null) return;
    
    while (node.hasChildNodes()) {
        node.removeChild(node.lastChild); // backward looping
    }
}
/*****************************************************************************/
/* returns the absolute x- or y-position of an element */
function getPosition(element, attribute) {

    var iPos = 0;
    while (element) {
        iPos += element[attribute];
        element = element.offsetParent;
    }
    return iPos;
}

/*****************************************************************************/
/* form events helpers */
/*****************************************************************************/
/* handler for keypress: allows digits only */
function numbersOnly(e) {

    var iCode = e.charCode ? e.charCode : e.keyCode;
    if (iCode != 8 && iCode != 9) { // skip backspace and tab
        if (iCode < 48 || iCode > 57) return false;
    }
    return true;
}
/*****************************************************************************/
/* handler for keypress: allows digits only, backspace and limited symbols */
function looseNumbersOnly(e) {
   
    var iCode = e.charCode ? e.charCode : e.keyCode;
    
    /* allows backspace, tab, space, +, -, (, ) */
    if (iCode == 8 || iCode==9 || iCode == 32 || iCode == 40 || iCode == 41 || iCode == 45 || iCode == 43 || iCode==37 || iCode==39) 
        return true;
    
    return (iCode >= 48 && iCode <= 57)
}
/*****************************************************************************/
/* handler for kepup: enforce max length restriction */
function maxLength(ip, length) {
    
    if (ip.value.length > length)
        ip.value = ip.value.substring(0, length);
}
/*****************************************************************************/
function checkRange(ip, max) {

    var i = parseInt(ip.value);
    
    /* handle single digit */
    if (i <= 9 && i > max) ip.value = "";

    /* handle two or more digits */
    else if (i > max) ip.value = ip.value.substring(0, 1);

    else return;
}
/*****************************************************************************/
function fixYear(field) {

    var iYear = parseInt(field.value);
    var strYear = field.value;
    
    /* prepend a zero */
    if (strYear.length == 1) 
        strYear = "0" + strYear;

    if (iYear < 11)
        field.value = "20" + iYear.toString();
        
    else if (iYear < 100) 
        field.value = "19" + iYear.toString();

    else return;
}
/*****************************************************************************/
/* display error helpers */
/*****************************************************************************/
function hideDiv(div) {
    div.innerHTML = "";
    div.style.display = "none";
}
/*****************************************************************************/
function showDiv(div, msg) {
    div.innerHTML = msg;
    div.style.display = "inline";
}